<?php
class Solution {
    public function strStr($haystack,$needle)
    {
        $haystackLen = strlen($haystack);
        $needleLen = strlen($needle);

        if(empty($haystack) && empty($needle)) {
            return 0;
        }

        if(empty($haystack)) {
            return -1;
        }

        if(empty($needle)) {
            return 0;
        }

        if($needleLen > $haystackLen) {
            return -1;
        }

        if($haystackLen <1 || $needleLen < 1) {
            return -1;
        }

        $i=0;
        $j=0;
        while ($i < $haystackLen && $j < $needleLen) {
            if($haystack{$i} == $needle{$j}) {
                $i++;
                $j++;
            }  else {
                //如果不匹配，就回退，从第一次匹配的下一个开始，
                echo "i:".$i.'-'.$j."\r\n";
                $i = $i-$j+1;
                $j = 0;
            }

            if($j == $needleLen) {
                return $i-$j;
            }
        }
        return -1;
    }
}
$haystack = "mississippi";
$needle = "issip";
$res = (new Solution())->strStr($haystack,$needle);
var_dump($res);